package com.group4.DB;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.group4.DAO.VideoCommentDAO;

public class VideoComment {
	
	private int ID;
	private int videoId;
	private String commentText;
	private int commentOwnerId;
	private Date commentDateTime;
	private int commentRate;
	private boolean commentVisible;

	static PreparedStatement ps = null;
	static ResultSet rs = null;
	
public static ArrayList<VideoCommentDAO> getAllVideosCommentsWithVideoID(int ID) {
		
		ArrayList<VideoCommentDAO> results = new ArrayList<VideoCommentDAO>();
		
		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}

		VideoCommentDAO videoComment = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_VideoComment where video_id = ?");

			ps.setInt(1, ID);

			rs = ps.executeQuery();

			while (rs.next()) {

				videoComment = new VideoCommentDAO();

				videoComment.setID(rs.getInt("ID"));
				videoComment.setVideoId(ID);
				videoComment.setCommentText(rs.getString("comment_text"));
				videoComment.setCommentOwnerId(rs.getInt("comment_ownerId"));
				videoComment.setCommentDateTime(rs.getDate("comment_dateTime"));
				videoComment.setCommentRate(rs.getInt("comment_rate"));
				videoComment.setCommentVisible(rs.getBoolean("comment_visible"));
				
				results.add(videoComment);
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getVideoCommentWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getVideoCommentWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getVideoCommentWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getVideoCommentWithID. ");
					e.printStackTrace();
				}
			}
		}
		return results;
	}

	public static VideoCommentDAO getVideoCommentWithID(int ID) {

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}

		VideoCommentDAO videoComment = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_VideoComment where ID = ?");

			ps.setInt(1, ID);

			rs = ps.executeQuery();

			while (rs.next()) {

				videoComment = new VideoCommentDAO();

				videoComment.setID(ID);
				videoComment.setVideoId(rs.getInt("video_id"));
				videoComment.setCommentText(rs.getString("comment_text"));
				videoComment.setCommentOwnerId(rs.getInt("comment_ownerId"));
				videoComment.setCommentDateTime(rs.getDate("comment_dateTime"));
				videoComment.setCommentRate(rs.getInt("comment_rate"));
				videoComment.setCommentVisible(rs.getBoolean("comment_visible"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getVideoCommentWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getVideoCommentWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getVideoCommentWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getVideoCommentWithID. ");
					e.printStackTrace();
				}
			}
		}
		return videoComment;
	}

	public static boolean addVideoComment(VideoCommentDAO videoComment) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			ps = ConnectionManager.conn
					.prepareStatement(" insert into tbl_VideoComment (video_id, comment_text, comment_ownerId, comment_dateTime, comment_rate, comment_visible)"
							+ " values (?, ?, ?, ?, ?, ?)");

			ps.setInt(1, videoComment.getVideoId());
			ps.setString(2, videoComment.getCommentText());
			ps.setInt(3, videoComment.getCommentOwnerId());
			ps.setDate(4, videoComment.getCommentDateTime());
			ps.setInt(5, videoComment.getCommentRate());
			ps.setBoolean(6, videoComment.isCommentVisible());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in addVideoComment. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in addVideoComment. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in addVideoComment. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public boolean updateVideoComment(VideoCommentDAO videoComment) {

		boolean result = false;
		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			VideoCommentDAO oldVideoComment = getVideoCommentWithID(videoComment.getID());

			if (oldVideoComment == null) {
				System.out.println("No VideoComment found with ID = " + videoComment.getID());
				return result;
			}

			ps = ConnectionManager.conn
					.prepareStatement("update tbl_VideoComment set videoid = ?, comment_text = ?, comment_ownerId = ?, comment_dateTime = ?, comment_rate = ?, comment_visible = ? where ID = ?");

			ps.setInt(2, videoComment.getVideoId());
			ps.setString(3, videoComment.getCommentText());
			ps.setInt(4, videoComment.getCommentOwnerId());
			ps.setDate(5, videoComment.getCommentDateTime());
			ps.setInt(6, videoComment.getCommentRate());
			ps.setBoolean(7, videoComment.isCommentVisible());
			ps.setInt(1, videoComment.getID());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in updateVideoComment. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in updateVideoComment. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in updateVideoComment. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean deleteVideoComment(int ID) {
		boolean result = false;
		
		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;

		try {
			VideoCommentDAO oldVideoComment = getVideoCommentWithID(ID);

			if (oldVideoComment == null) {
				System.out.println("No VideoComment found with ID = " + ID);
				return result;
			}
			
			ps = ConnectionManager.conn.prepareStatement("delete from tbl_VideoComment where ID = ?");

			ps.setInt(1, ID);

			ps.execute();
			
			result = true;

		} catch (SQLException e) {
			System.out.println("SQL Error in deleteVideoComment. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in deleteVideoComment. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in deleteVideoComment. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append("Video Comment [ID:").append(ID);
		sb.append("; video_id:").append(videoId);
		sb.append("; comment_text:").append(commentText);
		sb.append("; comment_ownerId:").append(commentOwnerId);
		sb.append("; comment_dateTime:").append(commentDateTime);
		sb.append("; comment_rate:").append(commentRate);
		sb.append("; comment_visible:").append(commentVisible);
		sb.append("]");

		return sb.toString();
	}

}
